package com.lee.dutylower.db.greendao;

import java.util.List;
import java.util.ArrayList;
import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.internal.SqlUtils;
import org.greenrobot.greendao.internal.DaoConfig;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseStatement;
import org.greenrobot.greendao.query.Query;
import org.greenrobot.greendao.query.QueryBuilder;

import com.lee.dutylower.db.greenEntity.SoldierSentry;
import com.lee.dutylower.db.greenEntity.Squad;

import com.lee.dutylower.db.greenEntity.Soldier;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table "SOLDIER".
*/
public class SoldierDao extends AbstractDao<Soldier, Long> {

    public static final String TABLENAME = "SOLDIER";

    /**
     * Properties of entity Soldier.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties {
        public final static Property SID = new Property(0, Long.class, "sID", true, "_id");
        public final static Property SquadID = new Property(1, Long.class, "squadID", false, "SQUAD_ID");
        public final static Property SName = new Property(2, String.class, "sName", false, "S_NAME");
        public final static Property Grade = new Property(3, Integer.class, "grade", false, "GRADE");
        public final static Property InUse = new Property(4, Integer.class, "inUse", false, "IN_USE");
        public final static Property GuardTimes = new Property(5, Integer.class, "guardTimes", false, "GUARD_TIMES");
    }

    private DaoSession daoSession;

    private Query<Soldier> sentry_SoldiersQuery;
    private Query<Soldier> squad_SoldiersQuery;

    public SoldierDao(DaoConfig config) {
        super(config);
    }
    
    public SoldierDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = daoSession;
    }

    /** Creates the underlying database table. */
    public static void createTable(Database db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "\"SOLDIER\" (" + //
                "\"_id\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: sID
                "\"SQUAD_ID\" INTEGER," + // 1: squadID
                "\"S_NAME\" TEXT," + // 2: sName
                "\"GRADE\" INTEGER," + // 3: grade
                "\"IN_USE\" INTEGER," + // 4: inUse
                "\"GUARD_TIMES\" INTEGER);"); // 5: guardTimes
    }

    /** Drops the underlying database table. */
    public static void dropTable(Database db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"SOLDIER\"";
        db.execSQL(sql);
    }

    @Override
    protected final void bindValues(DatabaseStatement stmt, Soldier entity) {
        stmt.clearBindings();
 
        Long sID = entity.getSID();
        if (sID != null) {
            stmt.bindLong(1, sID);
        }
 
        Long squadID = entity.getSquadID();
        if (squadID != null) {
            stmt.bindLong(2, squadID);
        }
 
        String sName = entity.getSName();
        if (sName != null) {
            stmt.bindString(3, sName);
        }
 
        Integer grade = entity.getGrade();
        if (grade != null) {
            stmt.bindLong(4, grade);
        }
 
        Integer inUse = entity.getInUse();
        if (inUse != null) {
            stmt.bindLong(5, inUse);
        }
 
        Integer guardTimes = entity.getGuardTimes();
        if (guardTimes != null) {
            stmt.bindLong(6, guardTimes);
        }
    }

    @Override
    protected final void bindValues(SQLiteStatement stmt, Soldier entity) {
        stmt.clearBindings();
 
        Long sID = entity.getSID();
        if (sID != null) {
            stmt.bindLong(1, sID);
        }
 
        Long squadID = entity.getSquadID();
        if (squadID != null) {
            stmt.bindLong(2, squadID);
        }
 
        String sName = entity.getSName();
        if (sName != null) {
            stmt.bindString(3, sName);
        }
 
        Integer grade = entity.getGrade();
        if (grade != null) {
            stmt.bindLong(4, grade);
        }
 
        Integer inUse = entity.getInUse();
        if (inUse != null) {
            stmt.bindLong(5, inUse);
        }
 
        Integer guardTimes = entity.getGuardTimes();
        if (guardTimes != null) {
            stmt.bindLong(6, guardTimes);
        }
    }

    @Override
    protected final void attachEntity(Soldier entity) {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }

    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }    

    @Override
    public Soldier readEntity(Cursor cursor, int offset) {
        Soldier entity = new Soldier( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // sID
            cursor.isNull(offset + 1) ? null : cursor.getLong(offset + 1), // squadID
            cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // sName
            cursor.isNull(offset + 3) ? null : cursor.getInt(offset + 3), // grade
            cursor.isNull(offset + 4) ? null : cursor.getInt(offset + 4), // inUse
            cursor.isNull(offset + 5) ? null : cursor.getInt(offset + 5) // guardTimes
        );
        return entity;
    }
     
    @Override
    public void readEntity(Cursor cursor, Soldier entity, int offset) {
        entity.setSID(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setSquadID(cursor.isNull(offset + 1) ? null : cursor.getLong(offset + 1));
        entity.setSName(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2));
        entity.setGrade(cursor.isNull(offset + 3) ? null : cursor.getInt(offset + 3));
        entity.setInUse(cursor.isNull(offset + 4) ? null : cursor.getInt(offset + 4));
        entity.setGuardTimes(cursor.isNull(offset + 5) ? null : cursor.getInt(offset + 5));
     }
    
    @Override
    protected final Long updateKeyAfterInsert(Soldier entity, long rowId) {
        entity.setSID(rowId);
        return rowId;
    }
    
    @Override
    public Long getKey(Soldier entity) {
        if(entity != null) {
            return entity.getSID();
        } else {
            return null;
        }
    }

    @Override
    public boolean hasKey(Soldier entity) {
        return entity.getSID() != null;
    }

    @Override
    protected final boolean isEntityUpdateable() {
        return true;
    }
    
    /** Internal query to resolve the "soldiers" to-many relationship of Sentry. */
    public List<Soldier> _querySentry_Soldiers(Long sentryId) {
        synchronized (this) {
            if (sentry_SoldiersQuery == null) {
                QueryBuilder<Soldier> queryBuilder = queryBuilder();
                queryBuilder.join(SoldierSentry.class, SoldierSentryDao.Properties.SoldierId)
                    .where(SoldierSentryDao.Properties.SentryId.eq(sentryId));
                sentry_SoldiersQuery = queryBuilder.build();
            }
        }
        Query<Soldier> query = sentry_SoldiersQuery.forCurrentThread();
        query.setParameter(0, sentryId);
        return query.list();
    }

    /** Internal query to resolve the "soldiers" to-many relationship of Squad. */
    public List<Soldier> _querySquad_Soldiers(Long squadID) {
        synchronized (this) {
            if (squad_SoldiersQuery == null) {
                QueryBuilder<Soldier> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.SquadID.eq(null));
                squad_SoldiersQuery = queryBuilder.build();
            }
        }
        Query<Soldier> query = squad_SoldiersQuery.forCurrentThread();
        query.setParameter(0, squadID);
        return query.list();
    }

    private String selectDeep;

    protected String getSelectDeep() {
        if (selectDeep == null) {
            StringBuilder builder = new StringBuilder("SELECT ");
            SqlUtils.appendColumns(builder, "T", getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T0", daoSession.getSquadDao().getAllColumns());
            builder.append(" FROM SOLDIER T");
            builder.append(" LEFT JOIN SQUAD T0 ON T.\"SQUAD_ID\"=T0.\"_id\"");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }
    
    protected Soldier loadCurrentDeep(Cursor cursor, boolean lock) {
        Soldier entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;

        Squad squad = loadCurrentOther(daoSession.getSquadDao(), cursor, offset);
        entity.setSquad(squad);

        return entity;    
    }

    public Soldier loadDeep(Long key) {
        assertSinglePk();
        if (key == null) {
            return null;
        }

        StringBuilder builder = new StringBuilder(getSelectDeep());
        builder.append("WHERE ");
        SqlUtils.appendColumnsEqValue(builder, "T", getPkColumns());
        String sql = builder.toString();
        
        String[] keyArray = new String[] { key.toString() };
        Cursor cursor = db.rawQuery(sql, keyArray);
        
        try {
            boolean available = cursor.moveToFirst();
            if (!available) {
                return null;
            } else if (!cursor.isLast()) {
                throw new IllegalStateException("Expected unique result, but count was " + cursor.getCount());
            }
            return loadCurrentDeep(cursor, true);
        } finally {
            cursor.close();
        }
    }
    
    /** Reads all available rows from the given cursor and returns a list of new ImageTO objects. */
    public List<Soldier> loadAllDeepFromCursor(Cursor cursor) {
        int count = cursor.getCount();
        List<Soldier> list = new ArrayList<Soldier>(count);
        
        if (cursor.moveToFirst()) {
            if (identityScope != null) {
                identityScope.lock();
                identityScope.reserveRoom(count);
            }
            try {
                do {
                    list.add(loadCurrentDeep(cursor, false));
                } while (cursor.moveToNext());
            } finally {
                if (identityScope != null) {
                    identityScope.unlock();
                }
            }
        }
        return list;
    }
    
    protected List<Soldier> loadDeepAllAndCloseCursor(Cursor cursor) {
        try {
            return loadAllDeepFromCursor(cursor);
        } finally {
            cursor.close();
        }
    }
    

    /** A raw-style query where you can pass any WHERE clause and arguments. */
    public List<Soldier> queryDeep(String where, String... selectionArg) {
        Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg);
        return loadDeepAllAndCloseCursor(cursor);
    }
 
}
